const express = require('express');

const userService = require('../service/user');

const router = express.Router();

// 获取用户信息
router.get('/userinfo', async function (req, res, next) {
    let uid = req.uid;
    userService.getUserInfo(uid).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 修改用户信息
router.put('/userinfo', async function (req, res, next) {
    let uid = req.uid;
    let sex = req.body.sex;
    if (sex != '男' && sex != '女') {
        return res.status(400).json({ code: 400, msg: '性别错误', data: null });
    }
    let grade = req.body.grade;
    if (grade != '大一' && grade != '大二' && grade != '大三' && grade != '大四') {
        return res.status(400).json({ code: 400, msg: '年级错误', data: null });
    }
    let college = req.body.college;
    if (!college) {
        return res.status(400).json({ code: 400, msg: '学校不允许为空', data: null });
    }
    let subCollege = req.body.subCollege;
    if (!subCollege) {
        return res.status(400).json({ code: 400, msg: '学院不允许为空', data: null });
    }
    let major = req.body.major;
    if (!major) {
        return res.status(400).json({ code: 400, msg: '专业不允许为空', data: null });
    }
    let username = req.body.username;
    if (!username) {
        return res.status(400).json({ code: 400, msg: '用户名不允许为空', data: null });
    }
    let bio = req.body.bio || '';
    userService.updateUserInfo(uid, sex, grade, college, subCollege, major, username, bio).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 获取关注数和被关注数
router.get('/follow-count', async function (req, res, next) {
    let uid = req.uid;
    userService.followCount(uid).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 关注或取消关注用户接口
router.post('/follow', async function (req, res, next) {
    let uid = req.uid;
    let followuid = req.body.followuid;
    let is_like = req.body.is_like;
    if (!followuid) {
        return res.status(400).json({ code: 400, msg: '参数错误', data: null });
    }
    if (uid == followuid) {
        return res.status(400).json({ code: 400, msg: '不能关注自己', data: null });
    }
    userService.follow(uid, followuid, is_like).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 懒加载获取关注列表
router.get('/follow-list', async function (req, res, next) {
    let uid = req.uid;
    let cursor = req.query.cursor || null;
    let limit = parseInt(req.query.limit) || 20;
    if (limit > 50) {
        limit = 50;
    }
    userService.getFollowList(uid, cursor, limit).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 懒加载获取粉丝列表
router.get('/fans-list', async function (req, res, next) {
    let uid = req.uid;
    let cursor = req.query.cursor || null;
    let limit = parseInt(req.query.limit) || 20;
    if (limit > 50) {
        limit = 50;
    }
    userService.getFansList(uid, cursor, limit).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 修改用户头像接口
router.put('/avatar', async function (req, res, next) {
    let uid = req.uid;
    let img = req.body.img;
    if (!img) {
        res.status(500).json({ code: 500, msg: '请上传图片', data: null });
        return;
    }
    // 判断图片格式是否为 base64
    if (!img.startsWith('data:image')) {
        return res.status(400).json({ code: 400, msg: '图片格式错误, 应是base64编码的dataURL数据', data: null });
    }
    // 提取 base64 数据部分 再次判空
    let base64Data = img.split(',')[1];
    if (!base64Data) {
        return res.status(400).json({ code: 400, msg: '图片格式错误, base64数据为空', data: null });
    }
    // 将 base64 数据转换为 Buffer
    let buffer = Buffer.from(base64Data, 'base64');
    // 判断 img 这个base64字符串占用的字节是否超过 2M, 超过直接返回失败
    if (buffer.length > 2 * 1024 * 1024) {
        return res.status(400).json({ code: 400, msg: '图片过大，请重新上传', data: null });
    }
    userService.updateAvatar(uid, buffer).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

module.exports = router;